package com.ape.service.impl;

import com.ape.entity.PageResult;
import com.ape.entity.dto.UserDto;
import com.ape.entity.po.UserPo;
import com.ape.dao.UserDao;
import com.ape.service.UserService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public int addUser(UserDto userDto) {
        UserPo userPo = new UserPo();
        BeanUtils.copyProperties(userDto, userPo);
        int flag = userDao.insert(userPo);
        return flag;
    }

    public int deleteUser(Integer id) {
        return userDao.deleteById(id);
    }

    @Override
    public PageResult<UserPo> getUserPages(UserDto userDto) {
        IPage<UserPo> page = new Page<>(userDto.getPageIndex(),userDto.getPageSize());
        IPage<UserPo> userPages = userDao.getUserPage(page);
        PageResult<UserPo>  pageResult = new PageResult<>();
        pageResult.loadData(userPages);
        return pageResult;
    }
}
